import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.print.DocFlavor.URL;
import javax.swing.*;
import javax.swing.border.Border;

class InterfaceLayeredPane extends JLayeredPane implements ActionListener{

    Interface menu;
    public static final int FRAME_WIDTH = 1024, FRAME_HEIGHT = 700;
    private String currentSong = "Click play to start selected song.";
    private String currentArtist = "No song playing.";
    private String songFileName = "", currentAlbum = "";
    private DecodeAndPlayAudio newPlayer;
    private LastFMContentGrabber grabber;
    private MusicSelector musicSelector;
    private JTextArea artist, song, topLeftContent, topMiddleContent, topRightContent;
    private JTextArea bottomLeftContent, bottomMiddleContent, bottomRightContent;
    private JLabel albumArt;
    private Lyrics lyricFinder;
    private JButton getImage;
    private JPanel artScroll;

    public InterfaceLayeredPane(Interface menu1){

        // Pane initialisation
        menu = menu1;
        setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
        setBackground(Color.WHITE);
        setOpaque(true);
        Font font = new Font("Verdana", Font.BOLD, 20);
        
        //Put the logo in the top corner
        java.net.URL u = this.getClass().getResource("pictures/companylogo.png");
        ImageIcon icon = new ImageIcon(u);
        JLabel logo = new JLabel(icon);
        int iconWidth = icon.getIconWidth();
        int iconHeight = icon.getIconHeight(); 
        logo.setSize(icon.getIconWidth(),icon.getIconHeight());
        logo.setLocation(0,0);
        
        // Couple of buttons 

        JButton playButton = new JButton("Play");
        playButton.setSize(100,40);
        int playLocationW = icon.getIconWidth() + 10;
        int playLocationH = (icon.getIconHeight() - 40) / 2;
        playButton.setLocation(playLocationW, playLocationH);
        playButton.setActionCommand("play");
        playButton.addActionListener(this);
        
        getImage = new JButton("Get Album Art");
        getImage.setSize(200,60);
        int imageLocationW = (686) + (339/2) - 100;
        int imageLocationH = icon.getIconWidth() + 10 + (305 / 2) - 80;
        getImage.setLocation(imageLocationW, imageLocationH);
        getImage.setActionCommand("get image");
        getImage.addActionListener(this);

        
        JButton stopButton = new JButton("Stop");
        stopButton.setSize(100,40);
        int stopLocationW = playLocationW + playButton.getWidth() + 10;
        stopButton.setLocation(stopLocationW, playLocationH);
        stopButton.setActionCommand("stop");
        stopButton.addActionListener(this);
        
        JButton libraryButton = new JButton("Library");
        libraryButton.setSize(100,40);
        int libraryLocationW = stopLocationW + stopButton.getWidth() + 10;
        libraryButton.setLocation(libraryLocationW, playLocationH);
        libraryButton.setActionCommand("library");
        libraryButton.addActionListener(this);
        
        //Create JTextArea for artist name
        artist = new JTextArea(1, 76);
        artist.setSize(FRAME_WIDTH - 220 - iconWidth, (iconHeight / 2) - 5);
        artist.setLocation(libraryLocationW + 110, 5);
        artist.setEditable(false);
        artist.setFont(font);
        artist.append(currentArtist);
        
        //Create JTextArea for song title
        song = new JTextArea(1, 76);
        song.setSize(artist.getWidth(),artist.getHeight());
        song.setLocation(libraryLocationW + 110, song.getHeight() + 10);
        song.setEditable(false);
        song.setFont(font);
        song.append(currentSong);
        
        // MUSIC SELECTOR
        musicSelector = new MusicSelector();
        
        //Create content holder
        topLeftContent = textSetup("Song Lyrics");
        JScrollPane topLeftScroll = scrollSetup(topLeftContent, 6, iconHeight + 10);
        
        topMiddleContent = textSetup("Artist Biography");
        JScrollPane topMiddleScroll = scrollSetup(topMiddleContent, 346, iconHeight + 10);
        
        topRightContent = textSetup("");
        JScrollPane topRightScroll = scrollSetup(topRightContent, 686, iconHeight + 10);
        
        bottomLeftContent = textSetup("Shouts");
        JScrollPane bottomLeftScroll = scrollSetup(bottomLeftContent, 6, iconHeight + 316);
        
        bottomMiddleContent = textSetup("");
        JScrollPane bottomMiddleScroll = scrollSetup(bottomMiddleContent, 346, iconHeight + 316);
        
        bottomRightContent = textSetup("");
        JScrollPane bottomRightScroll = scrollSetup(bottomRightContent, 686, iconHeight + 316);
        
        //Album art
        albumArt = new JLabel();
        artScroll = new JPanel();
        artScroll.add(albumArt);
        artScroll.setSize(339, 305);
        artScroll.setLocation(686, iconHeight + 10);
        

        
        //Content Grabber
        grabber = new LastFMContentGrabber();
        
        // Add all components
        add(logo, new Integer(2));
        add(artist, new Integer(2));
        add(song, new Integer(2));
        add(artScroll, new Integer(3));
        add(getImage, new Integer(3));
        add(musicSelector, new Integer(4));
        
        add(topLeftScroll, new Integer(2));
        add(topMiddleScroll, new Integer(2));
        add(topRightScroll, new Integer(2));
        add(bottomLeftScroll, new Integer(2));
        add(bottomMiddleScroll, new Integer(2));
        add(bottomRightScroll, new Integer(2));
        
        add(stopButton, new Integer(2));
        add(playButton, new Integer(2));
        add(libraryButton, new Integer(2));

        
    }

    //Create JTextArea in default content size
    JTextArea textSetup(String content) {
        JTextArea text = new JTextArea(13, 37);
        text.setBackground(Color.WHITE);
        text.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
        text.setEditable(false);      
        text.setSize(339,305);
        text.setText(content);
        return text;
    }

    //Create JScrollPane for content
    JScrollPane scrollSetup(JTextArea text, int locationW, int locationH) {
        JScrollPane scrollPane = new JScrollPane(text);
        scrollPane.setSize(339,305);
        scrollPane.setLocation(locationW, locationH);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        return scrollPane;
    }

    @SuppressWarnings("deprecation")
    public void actionPerformed(ActionEvent arg0) {

        if(arg0.getActionCommand().equals("play")){
            if (newPlayer != null) {
                newPlayer.stop();
            }
            newPlayer = new DecodeAndPlayAudio();
            songFileName = musicSelector.selectedFile();
            newPlayer.args = songFileName;
            newPlayer.start();
            System.out.println("playing song");
            
            //set song information
            currentArtist = "The Police";
            if (songFileName.equals("The Police - Every Breath You Take.mp3")) {
                currentSong = "Every Breath You Take";
                currentAlbum = "Synchronicity";
            } else {
                currentSong = "Message In A Bottle";
                currentAlbum = "Reggatta de Blanc";
            }
            
            //display song information
            artist.setText(currentArtist);
            song.setText(currentSong);
            
            getImage.setVisible(true);
            artScroll.setVisible(false);
            
            //search for lyrics
            lyricFinder = new Lyrics(currentSong, currentArtist);
            try {
                lyricFinder.getLyrics(topLeftContent);
            } catch (Exception e) {
                System.err.println("error with getting lyrics");
                e.printStackTrace();
            }
            topLeftContent.setCaretPosition(0);
            
            //get artist info
            topMiddleContent.setText(grabber.getArtistInfoString(currentArtist));
            topMiddleContent.setCaretPosition(0);
            
            //get shouts
            bottomLeftContent.setText(grabber.getShoutsString(currentArtist, currentSong));
            bottomLeftContent.setCaretPosition(0);
            
            getImage.setVisible(true);

        }
        if(arg0.getActionCommand().equals("get image")){
            //show the library
            ImageIcon art = new ImageIcon(toImage(grabber.getAlbumArt(currentArtist, currentAlbum)));
            Image scaledImage = getScaledImage(art.getImage(), 339, 305);
            ImageIcon newArt = new ImageIcon(scaledImage);
            albumArt.setIcon(newArt);
            getImage.setVisible(false);
            artScroll.setVisible(true);
            repaint();
        }
        if(arg0.getActionCommand().equals("library")){
            //show the library
            if (musicSelector.isVisible() == true) musicSelector.setVisible(false);
            else musicSelector.setVisible(true);
            System.out.println("changing library state");
            repaint();
        }
        if(arg0.getActionCommand().equals("stop")){
            //stop
            newPlayer.stop();
            currentSong = "Click play to start selected song.";
            currentArtist = "No song playing.";
            artist.setText(currentArtist);
            song.setText(currentSong);
            topLeftContent.setText("");
            topMiddleContent.setText("");
            bottomLeftContent.setText("");
            albumArt.setIcon(null);
            System.out.println("stopping song");
        }
    }

    public static Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
     }
    
    public static Image toImage(BufferedImage bufferedImage) {
        return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
    }
}